# python async

"""
网络模型有很多种，为了实现高并发也有很多方案，多线程，多进程。
无论是多进程，还是多进程，io的调度更多取决于系统，而协程的方式，
调度来自用户，用户可以在函数中yield一个状态。使用协程可以实现高效的
并发任务
"""
import time
import asyncio
#
# now = lambda : time.time()
# async def do_some_work(x):
#     print("Waitting:", x)
#
# start = now()
# coroutine = do_some_work(2)
# loop = asyncio.get_event_loop()
# loop.run_until_complete(coroutine)
#
# print("TIME:", now() - start)

# now = lambda : time.time()
#
# async def do_some_work(x):
#     print("Waitting: ",x)
#
# start = now()
# coroutine = do_some_work(2)
# loop = asyncio.get_event_loop()
# task = loop.create_task(coroutine)
# print(task)
# loop.run_until_complete(task)
# print(task)
# print('TIME: ', now() - start)
#

now = lambda : time.time()
async def do_some_work(x):
    print("Waitting: ", x)
    return "Done after{}s".format(x)

def callback(future):
    print("Callback:", future.result())

start = now()

coroutine = do_some_work(2)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(coroutine)
task.add_done_callback(callback)
loop.run_until_complete(task)
print("TIME: ", now() - start)













